﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;
namespace GUI.KhachHang
{
    public partial class frmChiTietGiaoDich : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion
        public frmChiTietGiaoDich()
        {
            InitializeComponent();
        }
        bool checkClose = false;
        string makho = "";
        string tenkho = "";
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        Entities.ChiTietGiaoDichKhachHang[] hienthi;
        Entities.ChiTietGiaoDichKhachHang save;
        Entities.SelectChiTietGiaoDichKhachHang temp;
        public frmChiTietGiaoDich(Entities.KhachHang khachhang)
        {
            InitializeComponent();
            try
            {

                date = Common.StaticVariable.InterfaceObject.SelectDate();
                batdau = ketthuc = date;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
               // temp = Common.StaticVariable.InterfaceObject.SelectChitietgiaodichkhachhang(khachhang, makho);
            }
            catch
            {
            }
        }

        #region lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLyComboBox();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch 
            {
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        checkClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion
        int i1;

        #region xử lý form
        void txtTimKiem_TextChanged(object sender, TextChangedEventArgs e)
        {
            ArrayList arr = new ArrayList();
            foreach (Entities.ChiTietGiaoDichKhachHang item in hienthi)
            {
                if (item.MaHoaDon.ToUpper().IndexOf(txtmahoadon.Text.ToUpper()) >= 0)
                    arr.Add(item);
            }
            dgvHienThi.DataSource = (Entities.ChiTietGiaoDichKhachHang[])arr.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            //ucHienThi.lbTongSo.Content = Common.Constants.TongSo + arr.Count;
        }

        void XuLyHDBanLe()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.HDBanLe item in temp.HDBanLe)
            {
                if (item.NgayLap.Date >= batdau.Date && item.NgayLap <= ketthuc.Date)
                {
                    Entities.ChiTietGiaoDichKhachHang ctgdkh = new Entities.ChiTietGiaoDichKhachHang();
                    ctgdkh.MaHoaDon = item.MaHDBanLe;
                    ctgdkh.NgayLap = item.NgayLap;
                    ctgdkh.TongTien = item.TongTienThanhToan;
                    ctgdkh.GhiChu = "";
                    array.Add(ctgdkh);
                }
            }
            hienthi = (Entities.ChiTietGiaoDichKhachHang[])array.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            dgvHienThi.DataSource = hienthi;
        }

        void XuLyHDBanBuon()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.HDBanBuon item in temp.HDBanBuon)
            {
                if (item.NgayLap.Date >= batdau.Date && item.NgayLap <= ketthuc.Date)
                {
                    Entities.ChiTietGiaoDichKhachHang ctgdkh = new Entities.ChiTietGiaoDichKhachHang();
                    ctgdkh.MaHoaDon = item.MaHDBanBuon;
                    ctgdkh.NgayLap = item.NgayLap;
                    ctgdkh.TongTien =item.TongTienThanhToan;
                    ctgdkh.GhiChu = item.GhiChu;
                    array.Add(ctgdkh);
                }
            }
            hienthi = (Entities.ChiTietGiaoDichKhachHang[])array.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            dgvHienThi.DataSource = hienthi;
        }

        void XuLyKHTLBL()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.KhachHangTraLaiBanLe item in temp.KhachHangTraLaiBanLe)
            {
                if (item.NgayNhap.Date >= batdau.Date && item.NgayNhap <= ketthuc.Date)
                {
                    Entities.ChiTietGiaoDichKhachHang ctgdkh = new Entities.ChiTietGiaoDichKhachHang();
                    ctgdkh.MaHoaDon = item.MaKhachHangTraLaiBanLe;
                    ctgdkh.NgayLap = item.NgayNhap;
                    ctgdkh.TongTien = item.TienBoiThuong;
                    ctgdkh.GhiChu = item.GhiChu;
                    array.Add(ctgdkh);
                }
            }
            hienthi = (Entities.ChiTietGiaoDichKhachHang[])array.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            dgvHienThi.DataSource = hienthi;
        }

        void XuLyKHTLBB()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.KhachHangTraLaiBanBuon item in temp.KhachHangTraLaiBanBuon)
            {
                if (item.NgayNhap.Date >= batdau.Date && item.NgayNhap <= ketthuc.Date)
                {
                    Entities.ChiTietGiaoDichKhachHang ctgdkh = new Entities.ChiTietGiaoDichKhachHang();
                    ctgdkh.MaHoaDon = item.MaKhachHangTraLaiBanBuon;
                    ctgdkh.NgayLap = item.NgayNhap;
                    ctgdkh.TongTien = item.TienBoiThuong;
                    ctgdkh.GhiChu = item.GhiChu;
                    array.Add(ctgdkh);
                }
            }
            hienthi = (Entities.ChiTietGiaoDichKhachHang[])array.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            dgvHienThi.DataSource = hienthi;
        }

        void XuLyPhieuTTCuaKH()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.PhieuTTCuaKH item in temp.PhieuTTCuaKH)
            {
                if (item.NgayLap.Date >= batdau.Date && item.NgayLap <= ketthuc.Date)
                {
                    Entities.ChiTietGiaoDichKhachHang ctgdkh = new Entities.ChiTietGiaoDichKhachHang();
                    ctgdkh.MaHoaDon = item.MaPhieuTTCuaKH;
                    ctgdkh.NgayLap = item.NgayLap;
                    ctgdkh.TongTien = item.TongThanhToan;
                    ctgdkh.GhiChu = item.GhiChu;
                    array.Add(ctgdkh);
                }
            }
            hienthi = (Entities.ChiTietGiaoDichKhachHang[])array.ToArray(typeof(Entities.ChiTietGiaoDichKhachHang));
            dgvHienThi.DataSource = hienthi;
        }

        void XuLyComboBox()
        {
            try
            {
                int i = cbxloaihoadon.SelectedIndex;
                if (i == 0)
                {
                    XuLyHDBanLe();
                }
                if (i == 1)
                {
                    XuLyHDBanBuon();
                }
                if (i == 2)
                {
                    XuLyKHTLBL();
                }
                if (i == 3)
                {
                    XuLyKHTLBB();
                }
                if (i == 4)
                {
                    XuLyPhieuTTCuaKH();
                }
            }
            catch
            {
            }
        }

        void cbbLoaiHoaDon_DropDownClosed(object sender, EventArgs e)
        {
            XuLyComboBox();
        }
       

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i1 = e.RowIndex;
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = cbxloaihoadon.SelectedIndex;
            //i1 = dgvHienThi.Ro;
            if (i == 0)
            {
                if (i1 < 0)
                    return;
                save = (Entities.ChiTietGiaoDichKhachHang)dgvHienThi.DataSource;
                Entities.HDBanLe te = new Entities.HDBanLe();
                foreach (Entities.HDBanLe item in temp.HDBanLe)
                {
                    if (save.MaHoaDon == item.MaHDBanLe)
                    {
                        te = item;
                        break;
                    }
                }
                //if ((Common.TitleName.XuLyBanLe))
                //{
                BanLe.frmXuLy frm = new BanLe.frmXuLy(te);

                frm.MyGetData = new BanLe.frmXuLy.GetString(new BanLe.frmQuanLy().GetValue);
                //frm.Title = Common.TitleName.XuLyBanLe;
                //Common.MenuStatus.Insert(Common.uti.sb, frm);
                frm.Show();
                //}

            }
            if (i == 1)
            {
                if (i1 < 0)
                    return;
                save = (Entities.ChiTietGiaoDichKhachHang)dgvHienThi.DataSource;
                Entities.HDBanBuon te = new Entities.HDBanBuon();
                foreach (Entities.HDBanBuon item in temp.HDBanBuon)
                {
                    if (save.MaHoaDon == item.MaHDBanBuon)
                    {
                        te = item;
                        break;
                    }
                }
                //if (Common.MenuStatus.Check(Common.TitleName.XuLyBanBuon))
                //{
                BanBuon.frmXuLy frm = new BanBuon.frmXuLy(te);
                //frm.MyGetData = new BanBuon.frmXuLy.GetString(new BanBuon.frmQuanLy().GetValue);
                //frm.Select = new BanBuon.frmXuLy.SelectData(new BanBuon.frmQuanLy().SelectData);
                //frm.Title = Common.TitleName.XuLyBanBuon;
                //Common.MenuStatus.Insert(Common.uti.sb, frm);
                frm.Show();
                //}

            }
            if (i == 2)
            {
                if (i1 < 0)
                    return;
                save = (Entities.ChiTietGiaoDichKhachHang)dgvHienThi.DataSource;
                Entities.KhachHangTraLaiBanLe te = new Entities.KhachHangTraLaiBanLe();
                foreach (Entities.KhachHangTraLaiBanLe item in temp.KhachHangTraLaiBanLe)
                {
                    if (save.MaHoaDon == item.MaKhachHangTraLaiBanLe)
                    {
                        te = item;
                        break;
                    }
                }
                //if (Common.MenuStatus.Check(Common.TitleName.XuLyKhachHangTraLaiBanLe))
                //{
                // KhachHangTraLaiBanLe.frmXuLy frm = new KhachHangTraLaiBanLe.frmXuLy(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanLe, te);
                // frm.MyGetData = new GUI.KhachHangTraLaiBanLe.frmXuLyTraLaiBanLe.GetString(new KhachHangTraLaiBanLe.frmQuanLyKhachHangTraLaiBanLe().GetValue);
                //frm.Title = Common.TitleName.XuLyKhachHangTraLaiBanLe;
                //Common.MenuStatus.Insert(Common.uti.sb, frm);
                //  frm.Show();
                //}

            }
            if (i == 3)
            {
                if (i1 < 0)
                    return;
                save = (Entities.ChiTietGiaoDichKhachHang)dgvHienThi.DataSource;
                Entities.KhachHangTraLaiBanBuon te = new Entities.KhachHangTraLaiBanBuon();
                foreach (Entities.KhachHangTraLaiBanBuon item in temp.KhachHangTraLaiBanBuon)
                {
                    if (save.MaHoaDon == item.MaKhachHangTraLaiBanBuon)
                    {
                        te = item;
                        break;
                    }
                }
                //if (Common.MenuStatus.Check(Common.TitleName.XuLyKhachHangTraLaiBanBuon))
                //{
                //    KhachHangTraLaiBanBuon.frmXuLyTraLaiBanBuon frm = new KhachHangTraLaiBanBuon.frmXuLyTraLaiBanBuon(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanBuon, te);
                //    frm.MyGetData = new GUI.KhachHangTraLaiBanBuon.frmXuLyTraLaiBanBuon.GetString(new KhachHangTraLaiBanBuon.frmQuanLyTraLaiBanBuon().GetValue);
                //    frm.Title = Common.TitleName.XuLyKhachHangTraLaiBanLe;
                //    Common.MenuStatus.Insert(Common.uti.sb, frm);
                //    frm.Show();
                //}
            }
            if (i == 4)
            {
                if (i1 < 0)
                    return;
                save = (Entities.ChiTietGiaoDichKhachHang)dgvHienThi.DataSource;
                Entities.PhieuTTCuaKH te = new Entities.PhieuTTCuaKH();
                foreach (Entities.PhieuTTCuaKH item in temp.PhieuTTCuaKH)
                {
                    if (save.MaHoaDon == item.MaPhieuTTCuaKH)
                    {
                        te = item;
                        break;
                    }
                }
                //if (Common.MenuStatus.Check(Common.TitleName.XửLyPhieuTTKH))
                //{
                PhieuTTCuaKH.frmXuLy frm = new PhieuTTCuaKH.frmXuLy(te, Common.Constants.Update);
                frm.Select = new PhieuTTCuaKH.frmXuLy.SelectData(new PhieuTTCuaKH.frmQuanLy().SelectData);
                //frm.Title = Common.TitleName.XửLyPhieuTTKH;
                //Common.MenuStatus.Insert(Common.uti.sb, frm);
                frm.Show();
                //}

            }

        }
         #endregion

        private void btnloc_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
    }
}
